package com.useeinfo.oa.modules.base.biz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.AliJsonObjectCover;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.dao.AuditFlowDao;
import com.useeinfo.oa.modules.base.entity.*;
import com.useeinfo.oa.modules.base.enums.NodeTypeEnum;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/7 15:50
 * @Description:
 */
@Service
public class AuditFlowBiz extends CrudBiz<AuditFlowDao, AuditFlow> {
    @Autowired
    private AuditNodeBiz auditNodeBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;
    @Autowired
    private EnterpriseBiz enterpriseBiz;

    /**
     * 创建工单时完善审批流将本人塞入
     */
    public String updateFlowBySelf(long enterpriseId,long userId,String flow){
        JSONArray jsonArray = JSON.parseArray(flow);
        SystemUser systemUser = systemUserBiz.findModel(userId);
        Enterprise enterprise = enterpriseBiz.findModel(enterpriseId);
        SystemUserOrganization systemUserOrganization = systemUser.getOrganization(enterprise, systemUser.getSystemUserOrganizationSet());
        String organizationId = "";
        if(systemUserOrganization != null){
            organizationId = StringConverters.null2String(systemUserOrganization.getOrganization().getId());
        }

        if(StringUtils.isNotEmpty(flow)&&jsonArray!=null){
            for(int i = 0;i < jsonArray.size();i++){
                JSONObject j = jsonArray.getJSONObject(i);
                String nodeTypeEnum = AliJsonObjectCover.ObjectString(j, "nodeTypeEnum");
                if(NodeTypeEnum.SELF.getCode().equals(nodeTypeEnum)){
                    JSONObject node = new JSONObject();
                    node.put("organizationId",organizationId);
                    node.put("relationId",StringConverters.null2String(userId));
                    node.put("nodeTypeEnum","USER");
                    jsonArray.set(i,node);
                }
            }
        }
        return jsonArray.toString();
    }

    public JSONObject setFlow(AuditNode auditNode) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("organizationId", auditNode.getOrganizationId());
        jsonObject.put("nodeTypeEnum", auditNode.getNodeTypeEnum().getCode());
        jsonObject.put("relationId", auditNode.getRelationId());
        return jsonObject;
    }

    public int updateDefault(String moduleId, String auditType) {
        return dao.updateDefault(moduleId, auditType);
    }

    //保存完整审批节点
    public void saveAllTree(String flowId) {
        AuditFlow auditFlow = dao.findModel(StringConverters.null2Long(flowId));
        JSONArray jsonArray = new JSONArray();
        AuditNode auditNode = auditNodeBiz.getFirstNode(flowId);
        if (auditNode != null) {
            jsonArray.add(setFlow(auditNode));
            while (auditNodeBiz.getNodeByParentId(auditNode.getId(), flowId) != null) {
                if (auditNodeBiz.getNodeJSONByParentId(auditNode.getId(), flowId) != null) {
                    jsonArray.add(auditNodeBiz.getNodeJSONByParentId(auditNode.getId(), flowId));
                }
                auditNode = auditNodeBiz.getNodeByParentId(auditNode.getId(), flowId);
            }
            auditFlow.setFlow(jsonArray.toString());
            auditFlow.setLevel(jsonArray.size());
        } else {
            auditFlow.setLevel(0);
            auditFlow.setFlow(null);
        }
        dao.update(auditFlow);

    }

    public AuditFlow getDefaultFlow(String type, Enterprise enterprise,int flag){
        //1,维修管理，2维护保养，3巡检
        QueryParam queryParam = new QueryParam();
        if("complete".equals(type)){
            queryParam.getSqlMap().put("complete",type);
        }else if("delay".equals(type)){
            queryParam.getSqlMap().put("delay",type);
        }else if("cancel".equals(type)){
            queryParam.getSqlMap().put("cancel",type);
        }
        String moduleType; //1,维修管理，2维护保养，3巡检
        if(flag==1){
            moduleType="REPAIR";
        }else{
            moduleType=flag==2?"MAINTAIN":"INSPECTION";
        }
        queryParam.getSqlMap().put("remark",moduleType);
        queryParam.getSqlMap().put("isDefaultV",StringConverters.null2String(true));
        queryParam.getSqlMap().put("enterpriseId",StringConverters.null2String(enterprise.getId()));
        List<AuditFlow> list = findList(queryParam);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

}
